/**
 * 
 */
package uit.comstore.controller;

import java.util.List;
import java.util.Map;

import uit.comstore.model.blo.AccountBLO;
import uit.comstore.model.entity.Account;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author XIII
 *
 */
@SuppressWarnings("serial")
public class AccountAction extends ActionSupport {

	private AccountBLO accountBLO;
	private List<Account> accountList;
	
	private String username;
	private String password;
	
	public AccountAction() {
		accountBLO = new AccountBLO();
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String login(){
		Account account = accountBLO.getAccount(username, password);
		if (account != null) {
			Map session = ActionContext.getContext().getSession();
			
			if (account.getRole().equals("admin")) {
				session.put("account", account);
			} else {
				session.put("account", "user");
			}
			return SUCCESS;
		}
		return SUCCESS;
	}
	
	public String register(){
		return SUCCESS;
	}
	
	public String logout() {
		ActionContext.getContext().getSession().clear();
		
		return SUCCESS;
	}

	public List<Account> getAccountList() {
		return accountList;
	}

	public void setAccountList(List<Account> accountList) {
		this.accountList = accountList;
	}
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}
